﻿using System;
using UnityEngine;
using UnityEngine.UI;

public class ToggleComp
{
    public bool IsOn
    {
        get
        {
            return toggle.isOn;
        }
        set
        {
            toggle.isOn = value;
        }
    }

    private Toggle toggle;
    private GameObject on;
    private GameObject off;

    public ToggleComp(GameObject gameObject, Action<bool> onValueChanged = null)
    {
        toggle = gameObject.GetComponent<Toggle>();
        on = gameObject.transform.Find("on").gameObject;
        off = gameObject.transform.Find("off").gameObject;
        toggle.onValueChanged.AddListener(b =>
        {
            on.SetActive(b);
            off.SetActive(!b);
            onValueChanged?.Invoke(b);
        });
    }

    public void Release()
    {
        toggle.onValueChanged.RemoveAllListeners();
    }
}
